import * as THREE from 'three';
import renderer from '@/config/render';
import camera from '@/config/camera'
import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls.js';
//控制器设置
const controls = new PointerLockControls(camera,renderer.domElement);

let container = document.getElementById('threeApp');
// 全局变量
var controlsEnabled = false;
let moveForward = false;
let moveBackward = false;
let moveLeft = false;
let moveRight = false;
let velocity = new THREE.Vector3(); //移动速度变量
let direction = new THREE.Vector3(); //移动的方向变量
var rotation = new THREE.Vector3(); //当前的相机朝向
var clock = new THREE.Clock();
let speed = 1000; //控制器移动速度
//声明射线
var downRaycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3( 0, -1, 0), 0, 10);
var horizontalRaycaster = new THREE.Raycaster(new THREE.Vector3(), new THREE.Vector3(), 0, 10);


let onKeyDown = function ( event ) {
    switch ( event.keyCode ) {
        case 38: // up
        case 87: // w
            moveForward = true;
            break;
        case 37: // left
        case 65: // a
            moveLeft = true; 
            break;
        case 40: // down
        case 83: // s
            moveBackward = true;
            break;
        case 39: // right
        case 68: // d
            moveRight = true;
            break;
    }
};
let onKeyUp = function ( event ) {
    switch( event.keyCode ) {
        case 38: // up
        case 87: // w
            moveForward = false;
            break;
        case 37: // left
        case 65: // a
            moveLeft = false;
            break;
        case 40: // down
        case 83: // s
            moveBackward = false;
            break;
        case 39: // right
        case 68: // d
            moveRight = false;
            break;
    }
};

function trigger(scene){
    if(controlsEnabled === true){
        //获取到控制器对象
        var control = controls.getObject();
        
        //获取刷新时间
        var delta = clock.getDelta();

        velocity.x -= velocity.x * 10.0 * delta;
        velocity.z -= velocity.z * 10.0 * delta;
        velocity.y -= velocity.y * 100.0 * delta; // 默认下降的速度

        //获取当前按键的方向并获取朝哪个方向移动
        direction.z = Number( moveForward ) - Number( moveBackward );
        direction.x = Number( moveLeft ) - Number( moveRight );
        //将法向量的值归一化
        direction.normalize();


        //判断鼠标按下的方向
        var m = new THREE.Matrix4();
        if(direction.z > 0){
            if(direction.x > 0){
                m.makeRotationY(Math.PI/4);
            }
            else if(direction.x < 0){
                m.makeRotationY(-Math.PI/4);
            }
            else{
                m.makeRotationY(0);
            }
        }
        else if(direction.z < 0){
            if(direction.x > 0){
                m.makeRotationY(Math.PI/4*3);
            }
            else if(direction.x < 0){
                m.makeRotationY(-Math.PI/4*3);
            }
            else{
                m.makeRotationY(Math.PI);
            }
        }
        else{
            if(direction.x > 0){
                m.makeRotationY(Math.PI/2);
            }
            else if(direction.x < 0){
                m.makeRotationY(-Math.PI/2);
            }
        }
        //给向量使用变换矩阵
        rotation.applyMatrix4(m);
        horizontalRaycaster.set( control.position , rotation );

        var horizontalIntersections = horizontalRaycaster.intersectObjects( scene.children, true);
        var horOnObject = horizontalIntersections.length > 0;

        //判断移动方向修改速度方向
        if(!horOnObject){
            if ( moveForward || moveBackward ) velocity.z -= direction.z * speed* delta;
            if ( moveLeft || moveRight ) velocity.x -= direction.x * speed* delta;
        }
        

        //复制相机的位置
        downRaycaster.ray.origin.copy( control.position );
        //获取相机靠下10的位置
        downRaycaster.ray.origin.y -= 10;
    
        //根据速度值移动控制器
        control.translateX( velocity.x * delta);
        control.translateY( velocity.y * delta  );
        control.translateZ( velocity.z * delta );

        //保证控制器的y轴在10以上
        if ( control.position.y < 10 ) {
            velocity.y = 0;
            control.position.y = 10;
        }
    }    
}

container.addEventListener('click',function(){
    controls.lock()
})


controls.addEventListener('lock',function(){
    controlsEnabled = true
    addEventListener( 'keydown', onKeyDown);
    addEventListener( 'keyup', onKeyUp);
})

controls.addEventListener('unlock',function(){
    controlsEnabled = false
    removeEventListener( 'keydown', onKeyDown );
    removeEventListener( 'keyup', onkeyup );
})






export default {
    controls:controls.getObject(),
    trigger
}